﻿using System;
using System.IO;
using System.Data;

//自定义命名空间
using DreamCube.Framework.DataAccess.Basic;
using DreamCube.Framework.DataAccess.SqlCe;

namespace RegexTester.UI.BLL
{
    public static class DBHelper
    {
        #region "属性"

        /// <summary>
        /// 数据库的路径
        /// </summary>
        private static String dbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "history.sdf");

        #endregion

        /// <summary>
        /// 获取一个数据库实例，如果当前路径存在数据库，则直接打开数据库，否则创建一个新的数据库
        /// </summary>
        /// <returns></returns>
        public static Database GetDB()
        {
            Database db = DbManager.GetDBByConnectionString(String.Format(Properties.Resources.connectionString, dbPath),
                                                     DBProviderType.SqlCe);

            if (!db.ExistTable("TestHistory"))
            {
                try
                {
                    String tableScript = "CREATE TABLE TestHistory"
                                         + "("
                                         + "id INT IDENTITY(1,1),"
                                         + "regex NVARCHAR(2000), /*正则表达式*/"
                                         + "source NVARCHAR(2000), /*需要验证的文本*/"
                                         + "testDatetime DATETIME /*测试的时间点*/"
                                         + ");";
                    db.ExecuteNonQuery(CommandType.Text, tableScript);
                }
                catch (Exception)
                { }
            }
            return db;
        }
    }
}
